# https://circleci.com/blog/persisting-data-in-workflows-when-to-use-caching-artifacts-and-workspaces/

version: 2.1

orbs:
  browser-tools: circleci/browser-tools@1.1.3

workflows:
  version: 2
  workflow:
    jobs:
      # The `build-tag` job will not run on all branches and will run only on the matched tags.
      - build-tag:
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
      # The `deploy-tag` job will not run on all branches and will run only on the matched tags.
      - deploy-tag:
          requires:
            - build-tag
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /v[0-9]+(\.[0-9]+)*/

# https://circleci.com/docs/2.0/executor-types/
jobs:
  build-tag:
    docker:
      - image: cimg/node:14.21-browsers
    steps:
      - checkout
      - browser-tools/install-browser-tools
      - run:
          name: Configuration
          command: |
            npm config set loglevel warn
            npm config set scripts-prepend-node-path auto
      - run:
          name: Setup environment variables
          command: |
            echo "export CI_BRANCH=${CIRCLE_BRANCH}" >> $BASH_ENV
            echo "export CI_BUILD_NUMBER=${CIRCLE_BUILD_NUM}" >> $BASH_ENV
            echo "export CI_COMMIT=${CIRCLE_SHA1}" >> $BASH_ENV
            echo "export CI_COMMIT_SHORT=${CIRCLE_SHA1:0:8}" >> $BASH_ENV
            echo "export CI_TAG=${CIRCLE_TAG}" >> $BASH_ENV
      - run:
          name: Install system packages
          command: |
            sudo apt-get update
      - run:
          name: Check version
          command: |
            git --version
            node --version
            java --version
            google-chrome --version
            npm --version
            yarn --version
      - run:
          name: Install packages
          command: |
            yarn
      - run:
          name: Build
          command: |
            yarn clean
            yarn lint
            yarn test
            if [[ -z "$CI_TAG" ]]; then
              yarn build-latest
            else
              yarn build
            fi
      - run:
          name: Archive build artifacts
          command: |
            tar -zcvf ci.tar.gz --directory dist cncjs
      - persist_to_workspace:
          # Must be an absolute path, or relative path from working_directory.
          # This is a directory on the container which is taken to be the root directory of the workspace.
          root: .
          paths:
            - ci.tar.gz
  deploy-tag:
    docker:
      - image: cimg/node:14.21
    steps:
      - setup_remote_docker
      - checkout
      - run:
          name: Setup environment variables
          command: |
            echo "export CI_BRANCH=${CIRCLE_BRANCH}" >> $BASH_ENV
            echo "export CI_TAG=${CIRCLE_TAG}" >> $BASH_ENV
      - attach_workspace:
          # Must be absolute path or relative path from working_directory
          at: workspace
      - run:
          name: Extract build artifacts
          command: |
            mkdir -p dist
            tar zxvf workspace/ci.tar.gz --directory dist
      - run:
          name: Build Docker image
          command: |
            DOCKER_BRANCH_TAG=$(echo $CI_TAG | sed -e 's/\//-/g')
            DOCKER_REPO=cncjs/cncjs

            echo "DOCKER_BRANCH_TAG=$DOCKER_BRANCH_TAG"
            echo "DOCKER_REPO=$DOCKER_REPO"

            echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
            docker build -f Dockerfile -t $DOCKER_REPO:$DOCKER_BRANCH_TAG .
            docker images
            docker push $DOCKER_REPO:$DOCKER_BRANCH_TAG
            docker tag $DOCKER_REPO:$DOCKER_BRANCH_TAG $DOCKER_REPO:latest
            docker push $DOCKER_REPO:latest
